//
//  MNDBackMoneyRootViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/9/28.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDBackMoneyRootViewController.h"
#import "NSDate+JKExtension.h"
#import "MNDBackMoneyCalendarCell.h"
#import "UIImage+JKColor.h"
#import "MNDBackMonryCalendarModel.h"
#import "MNDBackMoneyHeaderView.h"
#import "MNDBackMoneyContentCell.h"
#import "MNDReturnListModel.h"
#import "UIButton+JKImagePosition.h"
#import "BAPickView_OC.h"
#import "MNDTipsViewController.h"
#import "MNDInvestmentDetailViewController.h"
#import "MNDCelebrationViewController.h"

@interface MNDBackMoneyRootViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, MNDBackMoneyContentCellDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *calendarCollectionView;

@property (weak, nonatomic) IBOutlet UICollectionView *contentCollectionView;

@property(nonatomic, strong) NSMutableArray *months;

/**
 有多少个月
 */
@property(nonatomic, assign) NSInteger monthCount;

/**
 是否手动触发滑动
 */
@property(nonatomic, assign) BOOL isDragging;

@property(nonatomic, strong) NSIndexPath *currentIndexPath;

@property(nonatomic, strong) MNDReturnListModel *listModel;

@property(nonatomic, strong) NSString *selectedDate;

@property(nonatomic, strong) UIBarButtonItem *monthButton;
@property(nonatomic, strong) UIButton *yearButton;

@property(nonatomic, weak) MNDTipsViewController *tipsVC;

@property(nonatomic, assign) BOOL isPush;

@end

@implementation MNDBackMoneyRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.monthCount = [self calculateMonths];
    
    if (self.isUnReturn) {
        self.title = @"待回款";
    } else {
        self.title = @"已回款";
    }
    
    self.selectedDate = [[NSDate date] jk_stringWithFormat:@"yyyy-MM"];
    
    [self initialRightBarbuttonItems];
    [self initialLeftBarButtonItems];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    //如果是从上一页返回，不需要重新请求数据
    if (self.isPush) {
        self.isPush = NO;
        return;
    }
    
    if (!self.isUnReturn) {
        
        if (!self.currentIndexPath) {
            self.currentIndexPath = [NSIndexPath indexPathForItem:self.months.count - 1 inSection:0];
        }
        
        [self.calendarCollectionView scrollToItemAtIndexPath:self.currentIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
        
        [self.contentCollectionView scrollToItemAtIndexPath:self.currentIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];

    } else {
        if (!self.currentIndexPath) {
            self.currentIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        }
    }
    
    MNDBackMonryCalendarModel *model = self.months[self.currentIndexPath.item];
    
    [self loadReturnList:[model.year integerValue] withMonth:[model.month integerValue]];
    
    //显示提示页
    [self showCalendarTipsView];
}

- (void)initialRightBarbuttonItems {
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = kScreenWidth < 414? -10 : -15;
    NSString *itemTitle = @"待回款";
    if (self.isUnReturn) {
        itemTitle = @"已回款";
    }
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:itemTitle
                                                             style:UIBarButtonItemStylePlain
                                                            target:self
                                                            action:@selector(toReturnListViewControllerAction:)];
    [item setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size:14.0]} forState:UIControlStateNormal];
    [item setTintColor:kColorBlack];
    
    if (@available(iOS 11.0, *)) {
        self.navigationItem.rightBarButtonItem = item;
    } else {
        self.navigationItem.rightBarButtonItems = @[spaceItem, item];
    }
}

- (void)toReturnListViewControllerAction:(id)sender {
    
    if (!self.isUnReturn) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[MNDBackMoneyRootViewController description] bundle:nil];
    MNDBackMoneyRootViewController *backMoneyVC = [storyboard instantiateViewControllerWithIdentifier:[MNDBackMoneyRootViewController description]];
    backMoneyVC.isUnReturn = !self.isUnReturn;
    
    [self.navigationController pushViewController:backMoneyVC animated:YES];
    
    self.isPush = YES;
}

- (void)initialLeftBarButtonItems {
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = kScreenWidth < 414? -10 : -15;
    
    UIImage *backImage = [UIImage imageNamed:@"navi_back_btn_black"];
    
    //创建返回按钮
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backImage
                                                                          style:UIBarButtonItemStylePlain
                                                                         target:self
                                                                         action:@selector(backAction:)];
    //设置返回按钮的颜色
    [backBarButtonItem setTintColor:kColorBackButtonColor];
    
    UIBarButtonItem *monthItem = [[UIBarButtonItem alloc] initWithTitle:@"09"
                                                             style:UIBarButtonItemStylePlain
                                                            target:nil
                                                            action:nil];
    [monthItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Medium" size:20.0]} forState:UIControlStateNormal];
    [monthItem setTintColor:kColorBlack];
    
    UIButton *yearButton = [UIButton buttonWithType:UIButtonTypeCustom];
    yearButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [yearButton setTitle:@"2017" forState:UIControlStateNormal];
    [yearButton setTitleColor:kColorAF forState:UIControlStateNormal];
    [yearButton setImage:[UIImage imageNamed:@"back_money_dropdown_arrow"] forState:UIControlStateNormal];
    [yearButton jk_setImagePosition:LXMImagePositionRight spacing:4];
    [yearButton sizeToFit];
    [yearButton addTarget:self action:@selector(selectedDateAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *yearItem = [[UIBarButtonItem alloc] initWithCustomView:yearButton];
    
    //将两个BarButtonItem都返回给NavigationItem
    if (@available(iOS 11.0, *)) {
        self.navigationItem.leftBarButtonItems = @[backBarButtonItem, monthItem, yearItem];
    } else {
        self.navigationItem.leftBarButtonItems = @[spaceItem, backBarButtonItem, monthItem, yearItem];
    }
    
    self.monthButton = monthItem;
    self.yearButton = yearButton;
}

- (void)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)selectedDateAction:(UIButton *)sender {
    
    WeakSelf(self);
    [BAKit_DatePicker ba_creatPickerViewWithType:BAKit_CustomDatePickerDateTypeYM
                                   configuration:^(BAKit_DatePicker *tempView) {
        
                                       MNDBackMonryCalendarModel *minModel = self.months.firstObject;
                                       MNDBackMonryCalendarModel *maxModel = self.months.lastObject;
                                       NSString *minDateStr = [NSString stringWithFormat:@"%@-%@", minModel.year, minModel.month];
                                       NSString *maxDateStr = [NSString stringWithFormat:@"%@-%@", maxModel.year, maxModel.month];
                                       
                                       NSDate *minDate = [NSDate jk_dateWithString:minDateStr format:@"yyyy-MM"];
                                       NSDate *maxDate = [NSDate jk_dateWithString:maxDateStr format:@"yyyy-MM"];
                                       
                                       NSDate *selectedDate = [NSDate jk_dateWithString:self.selectedDate format:@"yyyy-MM"];
                                       
                                       // 自定义：最小日期
                                       tempView.ba_minDate = minDate;
                                       // 自定义：最大日期
                                       tempView.ba_maxDate = maxDate;
                                       //默认选中的日期
                                       tempView.ba_defautDate = selectedDate;
                                       //不显示标题
                                       tempView.isShowTitle = NO;
                                       // 自定义：动画样式
                                       tempView.animationType = BAKit_PickerViewAnimationTypeBottom;
                                       // 自定义：pickView 文字颜色
                                       tempView.ba_pickViewTextColor = kColorBlack;
                                       // 自定义：pickView 文字字体
                                       tempView.ba_pickViewFont = [UIFont systemFontOfSize:18];
                                       // 定制按钮颜色
                                       tempView.ba_buttonTitleColor_sure = kColorDarkOrange;
                                       
                                       tempView.ba_buttonTitleColor_cancle = kColorBlack;
                                       
                                       // 定制 toolBar 背景颜色
                                       tempView.ba_backgroundColor_toolBar = kColorF4;
                                       
                                   } block:^(NSString *resultString) {
                                       
                                       if (![weakself.selectedDate isEqualToString:resultString]) {
                                           weakself.selectedDate = resultString;
                                           NSString *year = [resultString substringToIndex:4];
                                           NSString *month = [resultString substringWithRange:NSMakeRange(5, 2)];
                                           
                                           __block NSInteger index = 0;
                                           
                                           [weakself.months enumerateObjectsUsingBlock:^(MNDBackMonryCalendarModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
                                               if ([model.year isEqualToString:year] && [model.month isEqualToString:month]) {
                                                   index = idx;
                                               }
                                           }];
                                           MNDBackMonryCalendarModel *selectedModel = self.months[index];
                                           
                                           [weakself.months enumerateObjectsUsingBlock:^(MNDBackMonryCalendarModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
                                               if ([selectedModel isEqual: model]) {
                                                   model.isSelected = YES;
                                                   NSLog(@"selected: %@-%@", model.year, model.month);
                                               } else {
                                                   model.isSelected = NO;
                                               }
                                           }];
                                           weakself.currentIndexPath = [NSIndexPath indexPathForItem:index inSection:0];
                                           [weakself.calendarCollectionView scrollToItemAtIndexPath:weakself.currentIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
                                           [weakself.contentCollectionView scrollToItemAtIndexPath:weakself.currentIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
                                           
                                           [weakself.calendarCollectionView reloadData];
                                           
                                           [weakself loadReturnList:[year integerValue] withMonth:[month integerValue]];
                                       }
                                   }];
}

#pragma mark - Network

- (void)loadReturnList:(NSInteger )year withMonth:(NSInteger)month {
    
    
    NSDictionary *params = [MNDSign signedParamWithDict: @{@"status": @(self.isUnReturn ? -1 : 1),
                                                   @"y": @(year),
                                                   @"m": @(month)}];
    //加载过程中禁止滚动
    [self.calendarCollectionView setScrollEnabled:NO];
    [self.contentCollectionView setScrollEnabled:NO];
    
    WeakSelf(self);
    
    [APIManager sendRequestWithPath:API_USER_RETURN_LIST parameters:params method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            //数据加载完毕，恢复滚动
            [weakself.calendarCollectionView setScrollEnabled:YES];
            [weakself.contentCollectionView setScrollEnabled:YES];
            
            weakself.listModel = [MNDReturnListModel mj_objectWithKeyValues:responseModel.data];
            [weakself.contentCollectionView reloadData];
        } else {
            //数据加载完毕，恢复滚动
            [weakself.calendarCollectionView setScrollEnabled:YES];
            [weakself.contentCollectionView setScrollEnabled:YES];
            
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
                
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
        //数据加载完毕，恢复滚动
        [weakself.calendarCollectionView setScrollEnabled:YES];
        [weakself.contentCollectionView setScrollEnabled:YES];
    }];
//    显示庆典日
    if (year == 2013 && month == 11) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           [self celebration];
        });
    }
}

#pragma mark - MNDBackMoneyContentCellDelegate

- (void)didSelectedWithTenderId:(NSString *)tenderId {
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"InvestmentRecord" bundle:nil];
    MNDInvestmentDetailViewController *recordVC = [storyboard instantiateViewControllerWithIdentifier:[MNDInvestmentDetailViewController description]];
    recordVC.tenderId = tenderId;
    [self.navigationController pushViewController:recordVC animated:YES];
    
    self.isPush = YES;
}

#pragma mark - UICollectionView Delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.months.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == self.calendarCollectionView) {
        static NSString * const cellIdentifier = @"CalendarCell";
        MNDBackMoneyCalendarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
        
        MNDBackMonryCalendarModel *model = self.months[indexPath.item];
        
        [cell configCellWithModel:model];
        
        if (model.isSelected) {
            [self.monthButton setTitle:model.month];
            [self.yearButton setTitle:model.year forState:UIControlStateNormal];
            [self.yearButton sizeToFit];
        }
        
        return cell;
    }
    
    if (collectionView == self.contentCollectionView) {
        static NSString * const cellIdentifier = @"ContentCell";
        
        MNDBackMoneyContentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
        
        cell.dateTime = self.selectedDate;
        cell.isUnReturn = self.isUnReturn;
        cell.delegate = self;
        
        if (indexPath == self.currentIndexPath) {
            cell.returnListModel = self.listModel;
        } else {
            cell.returnListModel = nil;
        }
        
        return cell;
    }

    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == self.calendarCollectionView) {
        MNDBackMonryCalendarModel *selectedModel = self.months[indexPath.item];
        
        for (MNDBackMonryCalendarModel *model in self.months) {
            if (model == selectedModel) {
                model.isSelected = YES;
                self.currentIndexPath = indexPath;
                self.selectedDate = [NSString stringWithFormat:@"%@-%@", model.year, model.month];
            } else {
                model.isSelected = NO;
            }
        }
        [self.contentCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
        [self loadReturnList:[selectedModel.year integerValue] withMonth:[selectedModel.month integerValue]];
        [collectionView reloadData];
    }
}


- (CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == self.calendarCollectionView) {
        return CGSizeMake(kScreenWidth / 6, 70);
    }
    
    if (collectionView == self.contentCollectionView) {
        return CGSizeMake(kScreenWidth, kScreenHeight - 160);
    }
    return CGSizeMake(kScreenWidth, 100);
}

#pragma mark - UIScrollView Delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"scrollViewDidEndDecelerating");
    if (scrollView == self.calendarCollectionView && self.isDragging) {
        self.isDragging = NO;
        
        NSArray *cells = self.calendarCollectionView.visibleCells;
        
        for (MNDBackMoneyCalendarCell *cell in cells) {
            NSIndexPath *indexPath = [self.calendarCollectionView indexPathForCell:cell];

            if (indexPath.item % 6 == 0) {
                MNDBackMonryCalendarModel *selectedModel = self.months[indexPath.item];
                for (MNDBackMonryCalendarModel *model in self.months) {
                    if (model == selectedModel) {
                        model.isSelected = YES;
                        self.currentIndexPath = indexPath;
                        self.selectedDate = [NSString stringWithFormat:@"%@-%@", model.year, model.month];
                        [self.contentCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
                    } else {
                        model.isSelected = NO;
                    }
                }
            }
        }
        MNDBackMonryCalendarModel *selectedModel = self.months[self.currentIndexPath.item];
        [self loadReturnList:[selectedModel.year integerValue] withMonth:[selectedModel.month integerValue]];
        [self.calendarCollectionView reloadData];
    }
    
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (scrollView == self.calendarCollectionView) {
        self.isDragging = YES;
    }
    NSLog(@"scrollViewDidEndDragging");
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    NSLog(@"scrollViewWillEndDragging");
    if (scrollView == self.contentCollectionView) {
        if (velocity.x > 0) {
            NSLog(@"右滑");
            NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForItem:self.currentIndexPath.item + 1 inSection:0];
            
            if (selectedIndexPath.item >= self.months.count) {
                return;
            }
            MNDBackMonryCalendarModel *selectedModel = self.months[selectedIndexPath.item];
            for (MNDBackMonryCalendarModel *model in self.months) {
                if (model == selectedModel) {
                    model.isSelected = YES;
                    self.currentIndexPath = selectedIndexPath;
                    self.selectedDate = [NSString stringWithFormat:@"%@-%@", model.year, model.month];
                    
                } else {
                    model.isSelected = NO;
                }
            }
            if (selectedIndexPath.item % 6 == 0) {
                NSIndexPath *scrollToIndexPath = [NSIndexPath indexPathForItem:selectedIndexPath.item + 5 inSection:0];
                if (scrollToIndexPath.item >= self.months.count) {
                    scrollToIndexPath = selectedIndexPath;
                }
                [self.calendarCollectionView scrollToItemAtIndexPath:scrollToIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
            }
            [self loadReturnList:[selectedModel.year integerValue] withMonth:[selectedModel.month integerValue]];
            [self.calendarCollectionView reloadData];
            
        } else if (velocity.x < 0) {
            
            NSLog(@"左滑");
            NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForItem:self.currentIndexPath.item - 1 inSection:0];
            if (selectedIndexPath.item < 0) {
                return;
            }
            MNDBackMonryCalendarModel *selectedModel = self.months[selectedIndexPath.item];
            for (MNDBackMonryCalendarModel *model in self.months) {
                if (model == selectedModel) {
                    model.isSelected = YES;
                    self.currentIndexPath = selectedIndexPath;
                    self.selectedDate = [NSString stringWithFormat:@"%@-%@", model.year, model.month];
                } else {
                    model.isSelected = NO;
                }
            }
            if (selectedIndexPath.item % 6 == 5) {
                NSIndexPath *scrollToIndexPath = [NSIndexPath indexPathForItem:selectedIndexPath.item - 5 inSection:0];
                [self.calendarCollectionView scrollToItemAtIndexPath:scrollToIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
            }
            [self loadReturnList:[selectedModel.year integerValue] withMonth:[selectedModel.month integerValue]];
            [self.calendarCollectionView reloadData];
            
//            if (selectedModel.year.integerValue == 2013 && selectedModel.month.integerValue == 11) {
//                NSLog(@"庆典日");
//                [self celebration];
//            }
        } else {
            NSLog(@"没动");
        }
    }
}

/**
 显示庆典日
 */
- (void)celebration {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[MNDCelebrationViewController description] bundle:nil];
    MNDCelebrationViewController *celebrationVC = [storyboard instantiateViewControllerWithIdentifier:[MNDCelebrationViewController description]];
    [KeyWindow addSubview:celebrationVC.view];
    [self addChildViewController:celebrationVC];
}

/**
 计算月份
 */
- (NSInteger )calculateMonths {
    //当前时间
    NSDate *currentDate = [NSDate date];
    
    //日期格式化为yyyy-MM-dd
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM";
    
    NSDate *fromDate = [dateFormatter dateFromString:@"2013-11"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate:currentDate];
    
    currentDate = [dateFormatter dateFromString:currentDateStr];
    
    NSDate *toDate = currentDate;
    
    if (self.isUnReturn) {
        fromDate = currentDate;
        //向后顺延3年
        toDate = [NSDate jk_offsetYears:3 fromDate:currentDate];
    }
    
    //当前日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //只取年和月
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth;
    
    //对比时间差
    NSDateComponents *dateComponents = [calendar components:unit fromDate:fromDate toDate:toDate options:NSCalendarWrapComponents];
    
    NSInteger months = dateComponents.year * 12 + dateComponents.month;
    
    return months;
}

- (NSMutableArray *)months {
    if (!_months) {
        _months = [NSMutableArray array];
        
        NSDate *currentDate = [NSDate date];
            
        NSInteger beginMonth = self.isUnReturn ? currentDate.jk_month : 11;
        NSInteger beginYear  = self.isUnReturn ? currentDate.jk_year  : 2013;
            
        for (NSInteger i = 0; i <= self.monthCount; i++) {
                
            NSInteger month = 1;
            if (beginMonth % 12 == 0) {
                month = 12;
            } else {
                month = beginMonth % 12;
            }
            if (month == 1 && beginMonth != 1) {
                beginYear ++;
            }
            
            MNDBackMonryCalendarModel *model = [[MNDBackMonryCalendarModel alloc] init];
            model.year = [NSString stringWithFormat:@"%zd", beginYear];
            model.month = [NSString stringWithFormat:@"%02zd", month];
            if (self.isUnReturn && i == 0) {
                model.isSelected = YES;
            }
            
            if (!self.isUnReturn && i == self.monthCount) {
                model.isSelected = YES;
            }
            
            [_months addObject:model];
            
            beginMonth ++;
        }
    }
    return _months;
}

- (void)showCalendarTipsView {
    BOOL tips = [UserDefaultsTool boolForKey:tipsCalendar];
    if (tips) {
        return;
    }
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MNDTipsViewController *tipsVC = [storyboard instantiateViewControllerWithIdentifier:[MNDTipsViewController description]];
    double naviHeight = 64.0;
    //iPhone X的NavigationBar高度为88
    if (kScreenHeight >= 812) {
        naviHeight = 88;
    }
    CGRect rect = CGRectMake(0, naviHeight, self.calendarCollectionView.frame.size.width, self.calendarCollectionView.frame.size.height - naviHeight);
    
    tipsVC.transparentFrame = rect;
    tipsVC.type = tipsCalendar;
    
    WeakSelf(self);
    tipsVC.closeTipsDidClick = ^{
        [weakself showCalendarDetailView];
    };
    
    [KeyWindow addSubview:tipsVC.view];
    [self.tabBarController addChildViewController:tipsVC];
    [UserDefaultsTool saveBool:YES forKey:tipsCalendar];
    
    self.tipsVC = tipsVC;
}

- (void)showCalendarDetailView {
    BOOL tips = [UserDefaultsTool boolForKey:tipsCalendarDetail];
    if (tips) {
        return;
    }
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MNDTipsViewController *tipsVC = [storyboard instantiateViewControllerWithIdentifier:[MNDTipsViewController description]];
    CGRect rect = CGRectMake(0, 130, self.calendarCollectionView.frame.size.width, self.calendarCollectionView.frame.size.height - 64);
    
    tipsVC.transparentFrame = rect;
    tipsVC.type = tipsCalendarDetail;
    
    [KeyWindow addSubview:tipsVC.view];
    [self.tabBarController addChildViewController:tipsVC];
    [UserDefaultsTool saveBool:YES forKey:tipsCalendarDetail];
    
    self.tipsVC = tipsVC;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
